package cn.iocoder.yudao.module.wms.controller.admin.customerclassify;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.customerclassify.vo.CustomerClassifyPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.customerclassify.vo.CustomerClassifyRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.customerclassify.vo.CustomerClassifySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.customerclassify.CustomerClassifyDO;
import cn.iocoder.yudao.module.wms.service.customerclassify.CustomerClassifyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 客户分类")
@RestController
@RequestMapping("/wms/customer-classify")
@Validated
public class CustomerClassifyController {

    @Resource
    private CustomerClassifyService customerClassifyService;

    @PostMapping("/create")
    @Operation(summary = "创建客户分类")
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:create')")
    public CommonResult<Long> createCustomerClassify(@Valid @RequestBody CustomerClassifySaveReqVO createReqVO) {
        return success(customerClassifyService.createCustomerClassify(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户分类")
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:update')")
    public CommonResult<Boolean> updateCustomerClassify(@Valid @RequestBody CustomerClassifySaveReqVO updateReqVO) {
        customerClassifyService.updateCustomerClassify(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:delete')")
    public CommonResult<Boolean> deleteCustomerClassify(@RequestParam("id") Long id) {
        customerClassifyService.deleteCustomerClassify(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:query')")
    public CommonResult<CustomerClassifyRespVO> getCustomerClassify(@RequestParam("id") Long id) {
        CustomerClassifyDO customerClassify = customerClassifyService.getCustomerClassify(id);
        return success(BeanUtils.toBean(customerClassify, CustomerClassifyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户分类分页")
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:query')")
    public CommonResult<PageResult<CustomerClassifyRespVO>> getCustomerClassifyPage(@Valid CustomerClassifyPageReqVO pageReqVO) {
        PageResult<CustomerClassifyDO> pageResult = customerClassifyService.getCustomerClassifyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerClassifyRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得客户分类精简列表", description = "主要用于前端的下拉选项和树状列表")
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:query')")
    public CommonResult<List<CustomerClassifyRespVO>> getInventoryClassifySimpleList() {
        List<CustomerClassifyDO> list = customerClassifyService.getCustomerClassifyList();
        return success(convertList(list, classify -> new CustomerClassifyRespVO()
                .setId(classify.getId()).setName(classify.getName()).setParentId(classify.getParentId())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户分类 Excel")
    @PreAuthorize("@ss.hasPermission('wms:customer-classify:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerClassifyExcel(@Valid CustomerClassifyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerClassifyDO> list = customerClassifyService.getCustomerClassifyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户分类.xls", "数据", CustomerClassifyRespVO.class,
                        BeanUtils.toBean(list, CustomerClassifyRespVO.class));
    }

}